package eureka.ui.transaction;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import eureka.domain.account.WebsiteUser;
import eureka.domain.transaction.Transaction;
import eureka.domain.transaction.TransactionState;
import eureka.exceptions.DatabaseException;
import eureka.service.transaction.impl.TransactionService;
import util.lang.RefValue;
import util.web.page.PageBean;

/**
 * Servlet implementation class ShowCompletedAsBuyer
 */
@WebServlet("/Transactions/ShowCompletedAsBuyer")
public class ShowCompletedAsBuyer extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private TransactionService ts = new TransactionService();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowCompletedAsBuyer() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		WebsiteUser user = (WebsiteUser) session.getAttribute("user");

		if (user == null) {
			response.sendRedirect(request.getAttribute("contextPath")
					+ "/Account/Login?redirect_url="
					+ URLEncoder.encode(request.getAttribute("contextPath")
							+ "/Transactions/ShowCompletedAsBuyer?" + request.getQueryString() , "UTF-8"));
			return;
		}

		String currentpagestr = request.getParameter("curPage");
		String pagesizestr = request.getParameter("pageSize");

		long currentpage = 0;
		long pagesize = 0;

		try {
			currentpage = Long.parseLong(currentpagestr);
			if (currentpage <= 0) {
				currentpage = 1;
			}
		} catch (Exception e) {
			currentpage = 1;
		}
		try {
			pagesize = Long.parseLong(pagesizestr);
			if (pagesize <= 0 || pagesize > 100) {
				pagesize = 20;
			}
		} catch (Exception e) {
			pagesize = 20;
		}

		RefValue<Long> count = new RefValue<Long>();
		PageBean<Transaction> bipage = new PageBean<Transaction>();
		bipage.setPageSize(pagesize);
		bipage.setCurrentPage(currentpage);
		List<Transaction> list = null;
		try {
			list = (List<Transaction>) ts.getTransactions(0, null,
					TransactionState.Completed, null, 0, user.getUID(), null,
					null, bipage.getRecordStart(), bipage.getPageSize(), count);
		} catch (DatabaseException e) {
			response.sendError(500);
			return;
		}

		bipage.setRecords(list, count.getValue());

		request.setAttribute("page", bipage);
		request.getRequestDispatcher("listcompletedasbuyer.jsp").forward(request,
				response);
	}
}
